package com.cadre.web.controller.officer;

import com.alibaba.fastjson.JSON;
import com.cadre.common.utils.R;
import com.cadre.common.utils.SecurityUtils;
import com.cadre.common.utils.annotation.SysLog;
import com.cadre.common.utils.common.GenerateUUID;
import com.cadre.common.utils.common.HttpContextUtils;
import com.cadre.common.utils.common.IPUtils;
import com.cadre.common.utils.oldutil.DateUtil;
import com.cadre.system.domain.officer.A01;
import com.cadre.system.domain.officer.A16;
import com.cadre.system.domain.officer.SysLogEntity;
import com.cadre.system.domain.vo.ESUtil;
import com.cadre.system.service.A01Service;
import com.cadre.system.service.A16Service;
import com.cadre.system.service.SysLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

@Api(tags = {"考核接口"})
@RequestMapping({"/profess/a16"})
@RestController
/* loaded from: A16Controller.class */
public class A16Controller {
    @Resource
    private A16Service a16Service;
    @Resource
    private A01Service a01Service;
    @Resource
    private SysLogService sysLogService;
    @Autowired
    RestHighLevelClient client;

    @GetMapping({"/info/{flowId}"})
    @ApiOperation("详情")
    public R info(@PathVariable("flowId") String flowId) {
        return R.ok().put("a16", (Object) ((A16) this.a16Service.getBaseMapper().selectById(flowId)));
    }

    @PostMapping({"/editA16Msg/{personid}"})
    @ApiOperation("操作A16")
    public R editA16Msg(@RequestBody List<A16> list, @PathVariable("personid") String personid) throws IOException {
        List<A16> listA16ES = new ArrayList<>();
        if (list.size() > 0) {
            personid = list.get(0).getPersonid();
            if (!(personid == null || personid == "")) {
                this.a16Service.deleteByPersonId(personid);
            }
            for (int i = 0; i < list.size(); i++) {
                A16 a16 = list.get(i);
                a16.setFlowid(GenerateUUID.getUUID());
                this.a16Service.insertSelective(a16);
                listA16ES.add(a16);
            }
            saveA0195Msg(personid, 3);
        } else if (!(personid == null || personid == "")) {
            this.a16Service.deleteByPersonId(personid);
        }
        Map<String, Object> source = this.client.get(new GetRequest(ESUtil.a01Index, personid), RequestOptions.DEFAULT).getSource();
        if (source != null) {
            A01 a01s = ESUtil.useObjectMapper(source);
            a01s.setA16List(listA16ES);
            this.client.index(new IndexRequest(ESUtil.a01Index).id(a01s.getFlowid()).source(JSON.toJSONString(a01s), XContentType.JSON), RequestOptions.DEFAULT);
        }
        return R.ok();
    }

    public void saveA0195Msg(String personid, Integer num) {
        Map<String, Object> map = new HashMap<>();
        map.put("personid", personid);
        String att = "";
        List<A16> list = this.a16Service.selectByList(map);
        if (list.size() < num.intValue() || list.size() <= 0) {
            for (int i = list.size() - 1; i >= 0; i--) {
                att = att + list.get(i).getA1601() + "年年度考核为" + list.get(i).getA1602() + "\n";
            }
        } else {
            for (int i2 = num.intValue() - 1; i2 >= 0; i2--) {
                att = att + list.get(i2).getA1601() + "年年度考核为" + list.get(i2).getA1602() + "\n";
            }
        }
        if (att.length() > 0) {
            att = att.substring(0, att.length() - 1);
        }
        A01 a01 = new A01();
        a01.setA0195(att);
        a01.setFlowid(personid);
        this.a01Service.updateById(a01);
    }

    @GetMapping({"/saveA0195"})
    @ApiOperation("更新A0195信息")
    public R saveA0195(String personid, Integer num) {
        Map<String, Object> map = new HashMap<>();
        map.put("personid", personid);
        String att = "";
        List<A16> list = this.a16Service.selectByList(map);
        if (list.size() < num.intValue() || list.size() <= 0) {
            for (int i = list.size() - 1; i >= 0; i--) {
                att = att + list.get(i).getA1601() + "年年度考核为" + list.get(i).getA1602() + "\n";
            }
        } else {
            for (int i2 = num.intValue() - 1; i2 >= 0; i2--) {
                att = att + list.get(i2).getA1601() + "年年度考核为" + list.get(i2).getA1602() + "\n";
            }
        }
        if (att.length() > 0) {
            att = att.substring(0, att.length() - 1);
        }
        A01 a01old = (A01) this.a01Service.getBaseMapper().selectById(personid);
        a01old.setA0195(att);
        if (this.a01Service.updateByPrimaryKeySelective(a01old) != 1) {
            return R.error();
        }
        try {
            SysLogEntity sysLog = new SysLogEntity();
            sysLog.setOperation("更新A0195信息");
            sysLog.setOperationtable("A01");
            sysLog.setOperationset("A01");
            a01old.setA0171(new byte[0]);
            sysLog.setParams(JSON.toJSONString(a01old).toLowerCase());
            sysLog.setOperationtype(1);
            a01old.setA0171(new byte[0]);
            sysLog.setOldparams(JSON.toJSONString(a01old).toLowerCase());
            sysLog.setMethod("saveA0195");
            sysLog.setTargetid(personid);
            sysLog.setIp(IPUtils.getIpAddr(HttpContextUtils.getHttpServletRequest()));
            sysLog.setUsername(SecurityUtils.getUsername());
            sysLog.setTime(100L);
            sysLog.setCreateDate(new Date());
            this.sysLogService.getBaseMapper().insert(sysLog);
        } catch (Exception e) {
        }
        return R.ok().put("data", (Object) att);
    }

    @GetMapping({"/list"})
    @ApiOperation("由人员personid获取信息")
    public R list(String personid) {
        Map<String, Object> map = new HashMap<>();
        map.put("personid", personid);
        return R.ok().put("data", (Object) this.a16Service.selectByList(map));
    }

    @GetMapping({"/listFirst3"})
    @ApiOperation("由人员personid获取信息前三条")
    public R listFirst3(String personid) {
        Map<String, Object> map = new HashMap<>();
        map.put("personid", personid);
        List<A16> list = this.a16Service.selectByList(map);
        List<A16> list3 = new ArrayList<>();
        if (list.size() > 0) {
            if (list.size() < 3) {
                list3 = list;
            }
            if (list.size() >= 3) {
                for (int i = 0; i < 3; i++) {
                    list3.add(list.get(i));
                }
            }
        }
        return R.ok().put("data", (Object) list3);
    }

    @GetMapping({"/savelist"})
    @ApiOperation("增(批量解析)")
    public R savelist(Integer startYear, Integer endYear, String ends, String personid) {
        if (endYear.intValue() > startYear.intValue()) {
            for (Integer i = startYear; i.intValue() <= endYear.intValue(); i = Integer.valueOf(i.intValue() + 1)) {
                A16 a16 = new A16();
                a16.setFlowid(GenerateUUID.getUUID());
                a16.setA1601(i.toString());
                a16.setA1602(ends);
                a16.setPersonid(personid);
                this.a16Service.insertSelective(a16);
                try {
                    SysLogEntity sysLog = new SysLogEntity();
                    sysLog.setOperation("保存考核信息");
                    sysLog.setOperationtable("A01");
                    sysLog.setOperationset("A16");
                    sysLog.setParams(JSON.toJSONString(a16).toLowerCase());
                    sysLog.setOperationtype(0);
                    sysLog.setOldparams(JSON.toJSONString(new A16()).toLowerCase());
                    sysLog.setMethod("savelist");
                    sysLog.setTargetid(personid);
                    sysLog.setIp(IPUtils.getIpAddr(HttpContextUtils.getHttpServletRequest()));
                    sysLog.setUsername(SecurityUtils.getUsername());
                    sysLog.setTime(100L);
                    sysLog.setCreateDate(new Date());
                    this.sysLogService.getBaseMapper().insert(sysLog);
                } catch (Exception e) {
                }
            }
            return R.ok();
        } else if (!endYear.equals(startYear)) {
            return R.error();
        } else {
            A16 a162 = new A16();
            a162.setFlowid(GenerateUUID.getUUID());
            a162.setA1601(startYear.toString());
            a162.setA1602(ends);
            a162.setPersonid(personid);
            this.a16Service.insertSelective(a162);
            try {
                SysLogEntity sysLog2 = new SysLogEntity();
                sysLog2.setOperation("保存考核信息");
                sysLog2.setOperationtable("A01");
                sysLog2.setOperationset("A16");
                sysLog2.setParams(JSON.toJSONString(a162).toLowerCase());
                sysLog2.setOperationtype(0);
                sysLog2.setOldparams(JSON.toJSONString(new A16()).toLowerCase());
                sysLog2.setMethod("savelist");
                sysLog2.setTargetid(personid);
                sysLog2.setIp(IPUtils.getIpAddr(HttpContextUtils.getHttpServletRequest()));
                sysLog2.setUsername(SecurityUtils.getUsername());
                sysLog2.setTime(100L);
                sysLog2.setCreateDate(new Date());
                this.sysLogService.getBaseMapper().insert(sysLog2);
            } catch (Exception e2) {
            }
            return R.ok();
        }
    }

    @PostMapping({"/saveListByMap"})
    @ApiOperation("增(批量解析) Map")
    public R saveListByMap(@RequestBody Map<String, Object> map) {
        Integer startYear = Integer.valueOf(Integer.parseInt((String) map.get("startYears")));
        Integer endYear = Integer.valueOf(Integer.parseInt((String) map.get("endYears")));
        String ends = (String) map.get("assessResult");
        ArrayList strList = (ArrayList) map.get("idArr");
        String[] idArr = new String[strList.size()];
        strList.toArray(idArr);
        for (String personid : idArr) {
            if (endYear.intValue() > startYear.intValue()) {
                for (Integer i = startYear; i.intValue() <= endYear.intValue(); i = Integer.valueOf(i.intValue() + 1)) {
                    Map<String, Object> pMap = new HashMap<>();
                    pMap.put("personid", personid);
                    List<A16> a16List = this.a16Service.selectByList(pMap);
                    for (int j = 0; j < a16List.size(); j++) {
                        try {
                            if (a16List.get(j).getA1601() != null && String.valueOf(i).equals(a16List.get(j).getA1601())) {
                                this.a16Service.deleteByPrimaryKey(a16List.get(j).getFlowid());
                            }
                        } catch (Exception e) {
                        }
                    }
                    A16 a16 = new A16();
                    a16.setFlowid(GenerateUUID.getUUID());
                    a16.setA1601(i.toString());
                    a16.setA1602(ends);
                    a16.setPersonid(personid);
                    this.a16Service.insertSelective(a16);
                    try {
                        SysLogEntity sysLog = new SysLogEntity();
                        sysLog.setOperation("保存考核信息");
                        sysLog.setOperationtable("A01");
                        sysLog.setOperationset("A16");
                        sysLog.setParams(JSON.toJSONString(a16).toLowerCase());
                        sysLog.setOperationtype(0);
                        sysLog.setOldparams(JSON.toJSONString(new A16()).toLowerCase());
                        sysLog.setMethod("savelist");
                        sysLog.setTargetid(personid);
                        sysLog.setIp(IPUtils.getIpAddr(HttpContextUtils.getHttpServletRequest()));
                        sysLog.setUsername(SecurityUtils.getUsername());
                        sysLog.setTime(100L);
                        sysLog.setCreateDate(new Date());
                        this.sysLogService.getBaseMapper().insert(sysLog);
                    } catch (Exception e2) {
                    }
                }
            } else if (endYear.equals(startYear)) {
                Map<String, Object> pMap2 = new HashMap<>();
                pMap2.put("personid", personid);
                List<A16> a16List2 = this.a16Service.selectByList(pMap2);
                for (int j2 = 0; j2 < a16List2.size(); j2++) {
                    try {
                        if (a16List2.get(j2).getA1601() != null && String.valueOf(endYear).equals(a16List2.get(j2).getA1601())) {
                            this.a16Service.deleteByPrimaryKey(a16List2.get(j2).getFlowid());
                        }
                    } catch (Exception e3) {
                    }
                }
                A16 a162 = new A16();
                a162.setFlowid(GenerateUUID.getUUID());
                a162.setA1601(startYear.toString());
                a162.setA1602(ends);
                a162.setPersonid(personid);
                this.a16Service.insertSelective(a162);
                try {
                    SysLogEntity sysLog2 = new SysLogEntity();
                    sysLog2.setOperation("保存考核信息");
                    sysLog2.setOperationtable("A01");
                    sysLog2.setOperationset("A16");
                    sysLog2.setParams(JSON.toJSONString(a162).toLowerCase());
                    sysLog2.setOperationtype(0);
                    sysLog2.setOldparams(JSON.toJSONString(new A16()).toLowerCase());
                    sysLog2.setMethod("savelist");
                    sysLog2.setTargetid(personid);
                    sysLog2.setIp(IPUtils.getIpAddr(HttpContextUtils.getHttpServletRequest()));
                    sysLog2.setUsername(SecurityUtils.getUsername());
                    sysLog2.setTime(100L);
                    sysLog2.setCreateDate(new Date());
                    this.sysLogService.getBaseMapper().insert(sysLog2);
                } catch (Exception e4) {
                }
            }
            saveA0195Msg(personid, 3);
        }
        return R.ok();
    }

    @PostMapping({"/save"})
    @SysLog(value = "新增考核信息", target = "A01", set = "A16", type = 0)
    @ApiOperation("增")
    public R save(@RequestBody A16 a16) {
        GenerateUUID.getUUID();
        a16.setFlowid(GenerateUUID.getUUID());
        this.a16Service.insertSelective(a16);
        return R.ok();
    }

    @PostMapping({"/update"})
    @SysLog(value = "修改考核信息", target = "A01", set = "A16", type = 1)
    @ApiOperation("改")
    public R update(@RequestBody A16 a16) {
        this.a16Service.updateByPrimaryKeySelective(a16);
        return R.ok();
    }

    @PostMapping({"/delete"})
    @SysLog(value = "删除考核信息", target = "A01", set = "A16", type = DateUtil.SUB_MONTH)
    @ApiOperation("删")
    public R delete(@RequestBody String[] flowids) {
        this.a16Service.deleteByPrimaryKey(flowids[0]);
        return R.ok();
    }
}
